<?php
//declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\common\lib\sms\Alsms;
use app\common\model\mysql\AdminUser;
use think\captcha\Captcha;
use think\Request;

class Login extends AdminBase
{
    public function initialize()
    {
        //如果用户已经登录直接跳转
        if($this->isLogin()){
           return $this->redirect(url('index/index'));
        }
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return view('index');
    }

    /**
     * 登录校验.
     *
     * @return \think\Response
     */
    public function check()
    {
        if(!$this->request->isPost()){// 判断是否是post提交
            return show(config('status.error'),'请求方式错误');
        }
        $name = $this->request->param('username','','trim');
        $pwd = $this->request->param('password','','trim');
        $captcha = $this->request->param('captcha','','trim');
        //1.判断是否为空
        $data=[
            'username' => $name,
            'password' => $pwd,
            'captcha' =>$captcha
        ];
        $validate = new \app\admin\validate\AdminUser();
        if(!$validate->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        //3.数据库查找用户  业务逻辑抽离到business层
        try{
            $result = \app\admin\business\AdminUserLogin::Login($data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        if($result){
            return  show(config('status.success'),'登录成功');;
        }else{
            return  show(config('status.error'),'登录失败');;
        }
    }
    public function cd()// 测试
    {
        Alsms::sendSms(15032853052,44444);
    }


}
